package advanced;

import java.util.concurrent.atomic.AtomicInteger;

public class Test1 {
    public static void main(String[] args) throws InterruptedException {

        AtomicInteger num = new AtomicInteger(0);
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                // 相当于num++,且是原子的
                num.getAndIncrement();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                // 相当于num++,且是原子的
                num.getAndIncrement();
            }
        });
        t1.start();// 启动 t1线程
        t2.start();// 启动 t2线程
        t1.join();
        t2.join();
        System.out.println(num);
    }
}
